use crate::dao;
use crate::runtime::error::MagicBoxError;
use crate::runtime::response::Code;
use crate::runtime::result::MagicBoxResult;

mod main {
    use refinery::embed_migrations;

    embed_migrations!("migrations/main");
}

mod components {
    pub(super) mod v2ray {
        use refinery::embed_migrations;

        embed_migrations!("migrations/components/v2ray");
    }
}

pub(crate) fn run() -> MagicBoxResult<()> {
    log::info!("执行主数据库迁移");
    let result = main::migrations::runner().run(dao::connection::main());
    if result.is_err() {
        return Err(MagicBoxError::new(Code::UndefinedError, format!("执行数据库迁移失败: {}", result.unwrap_err()).as_str()));
    }


    log::info!("执行组件-V2ray数据库迁移");
    let result = components::v2ray::migrations::runner().run(dao::connection::components::v2ray());
    if result.is_err() {
        return Err(MagicBoxError::new(Code::UndefinedError, format!("执行数据库迁移失败: {}", result.unwrap_err()).as_str()));
    }

    Ok(())
}